home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / boxdraw.exe / BOXDRAW.C < prev    next >
C/C++ Source or Header  |  1992-07-09  |  4KB  |  139 lines

  1.  
  2. /* remove this #define to use as a function in a program */
  3. #define DEMO
  4.  
  5. /*
  6.  * boxdraw - Copyright 1992, Edward Mulroy
  7.  * Authorized for private and commercial use
  8.  *
  9.  * This is designed for use with TC++, BC++ and TC 2.0
  10.  * It may be compiled as C or C++
  11.  */
  12.  
  13. #include <conio.h>
  14.  
  15. #include "boxdraw.h"
  16.  
  17.  
  18. typedef enum
  19.   {
  20.   UpLeft, Horiz, UpRight, Vert, LowLeft, LowRight
  21.   } box_index;
  22.  
  23. static char dble_box[]  = "\xC9\xCD\xBB\xBA\xC8\xBC"; /* "╔═╗║╚╝" */
  24. static char sngle_box[] = "\xDA\xC4\xBF\xB3\xC0\xD9"; /* "┌─┐│└┘" */
  25.  
  26. /*
  27.  * Draws a box with the corners given
  28.  *   [x1,y1] is the upper left corner of the box
  29.  *     [1,1] is the upper left corner of the screen
  30.  * "box_type" is 0 for a single line box, 1 for a double
  31.  * The box is drawn in the default color
  32.  *
  33.  * Return values are:
  34.  *  0=success  -1=invalid box_type  -2=invalid co-ordinates
  35.  */
  36. int draw_box( int x1, int y1, int x2, int y2, int box_type)
  37.   {
  38.   struct text_info ti;
  39.   char   line[ 133];
  40.   int    ix,iy, istop;
  41.   char  *draw_ptr;
  42.   char   ch;
  43.  
  44. #ifdef _NOCURSOR       /* TC 2.0 did not have _wscroll or _setcursortype() */
  45.   int old_wscroll = _wscroll; /* the 1st edition of TC++ did not have    */
  46.                               /* _wscroll declared, even 'though it was  */
  47.                               /* in there.  If that's the case with you  */
  48.                               /* put  extern int _wscroll;  at the top   */
  49.   _setcursortype( _NOCURSOR);
  50. #endif
  51.  
  52.   if ( box_type == single_border)       /* decide on which box type */
  53.     draw_ptr = sngle_box;               /* array to draw with       */
  54.   else if ( box_type == double_border)
  55.     draw_ptr = dble_box;
  56.   else
  57.     return -1;   /* indicate invalid box type */
  58.  
  59.   gettextinfo( &ti);     /* capture current screen, window and cursor info */
  60.  
  61.   if ( ((x2 - x1) < 2) || ((y2 - y1) < 2) || (x1 < ti.winleft) ||
  62.        (y1 < ti.wintop) || (x2 > ti.winright) || (y2 > ti.winbottom))
  63.     return -2;   /* indicate invalid box co-ordinates */
  64.  
  65.   /* make up a string containing the horizontal line for the top & bottom */
  66.   for ( istop = (x2 - x1 - 1), ix = 0, ch = draw_ptr[ Horiz]; ix < istop; ix++)
  67.     line[ ix] = ch;
  68.  
  69.   line[ ix] = '\0'; /* terminate the string */
  70.  
  71.   window( 1, 1, ti.screenwidth, ti.screenheight); /* use full-screen co-ord's */
  72.  
  73.   gotoxy( x1, y1);           /* draw the top */
  74.   putch( *draw_ptr);
  75.   cputs( line);
  76.   putch( draw_ptr[ UpRight]);
  77.  
  78.   gotoxy( x1, y2);           /* draw the bottom */
  79.   putch( draw_ptr[ LowLeft]);
  80.   cputs( line);
  81.   putch( draw_ptr[ LowRight]);
  82.  
  83.   for ( ch = draw_ptr[ Vert], iy = y1 + 1; iy < y2; iy++) /* draw the sides */
  84.     {
  85.     gotoxy( x1, iy);
  86.     putch( ch);
  87.     gotoxy( x2, iy);
  88.     putch( ch);
  89.     }
  90.  
  91.   /* restore the old window and cursor position */
  92.   window( ti.winleft, ti.wintop, ti.winright, ti.winbottom);
  93.   gotoxy( ti.curx, ti.cury);
  94.  
  95. #ifdef _NORMALCURSOR
  96.   _setcursortype( _NORMALCURSOR);
  97.   _wscroll = old_wscroll;
  98. #endif
  99.  
  100.  
  101.   return 0; /* return a value to indicate success */
  102.   }
  103.  
  104.  
  105. #ifdef DEMO
  106.  
  107. int main()
  108.   {
  109.   int i;
  110.  
  111.   clrscr();
  112.  
  113.   /* write the col and row numbers around one of the boxes */
  114.   for ( i = 10; i < 60; i += 10)
  115.     {
  116.     gotoxy( i, 1);
  117.     putch( (i / 10) + '0');
  118.     }
  119.  
  120.   gotoxy( 5,2);
  121.  
  122.   for ( i = 5; i < 51; i++)
  123.     putch( (i % 10) + '0');
  124.  
  125.   for ( i = 3; i <= 15; i++)
  126.     {
  127.     gotoxy( 51, i);
  128.     cprintf( "%2d", i);
  129.     }
  130.  
  131.   draw_box( 10, 20, 70, 23, double_border); /* draw two boxes */
  132.   draw_box( 5, 3, 50, 15, single_border);
  133.  
  134.   return 0;
  135.   }
  136.  
  137. #endif
  138.  
  139.